package com.guawazi.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.guawazi.MainActivity;
import com.guawazi.R;
import com.guawazi.base.BaseActivity;
import com.guawazi.utils.SharedPreferenceUtils;
import com.guawazi.widget.CommonTitleBar;
import com.rengwuxian.materialedittext.MaterialEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    CommonTitleBar titleBar;
    @BindView(R.id.phone_num_et)
    MaterialEditText phoneNumEt;
    @BindView(R.id.enter_password_et)
    MaterialEditText enterPasswordEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        titleBar.setLeftVisibility(View.GONE);
        titleBar.setTitle("登录");
    }

    private boolean verify() {
        if (TextUtils.isEmpty(phoneNumEt.getText().toString().trim())) {
            phoneNumEt.setError("手机号不能为空！");
            return false;
        } else if (phoneNumEt.getText().toString().trim().length() != 11) {
            phoneNumEt.setError("请输入正确的手机号！");
            return false;
        } else if (TextUtils.isEmpty(enterPasswordEt.getText().toString().trim())) {
            enterPasswordEt.setError("请输入密码！");
            return false;
        }
        return true;
    }

    @OnClick({R.id.login_btn, R.id.hide_btn, R.id.register_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                if (verify()) {
                    SharedPreferenceUtils.putValue(LoginActivity.this, "phoneNum", phoneNumEt.getText().toString().trim());
                    readyGoThenKill(MainActivity.class);
                }
                break;
            case R.id.hide_btn:
                SharedPreferenceUtils.putValue(LoginActivity.this, "phoneNum", "18800008128");
                readyGoThenKill(MainActivity.class);
                break;
            case R.id.register_btn:
                readyGo(RegisterActivity.class);
                break;
        }
    }
}
